package appapi.controller.oider;

import appapi.Request.Basic.OperItemReq;
import appapi.common.ResponseBean;
import appapi.entity.basic.OperatingItemEntity;
import appapi.entity.basic.OperatingTypeEntity;
import appapi.service.OperatingItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/26 10:09
 */
@Api(tags = "作业项目")
@RestController
@RequestMapping("/api/operatingItem")
public class OperatingItemController {
    private static final Logger logger = LoggerFactory.getLogger(OperatingItemController.class);
    @Autowired
    private OperatingItemService operatingItemService;
    /**
     * 列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取列表")
    public ResponseBean<OperatingItemEntity> list(@Valid OperItemReq req) {
        try {
            List<OperatingItemEntity> dataList = operatingItemService.getList(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取列表异常", e);
            return ResponseBean.fail("获取列表异常");
        }
    }

    /**
     * 准操作项目树
     */
    @GetMapping("/getTree")
    @ApiOperation(value = "准操作项目树")
    public ResponseBean<OperatingTypeEntity> getTree() {
        try {
            List<OperatingTypeEntity> dataList = operatingItemService.getTree();
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取准操作项目树异常", e);
            return ResponseBean.fail("获取准操作项目树异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "operItemId", dataType = "long", value = "项目ID",required = true)
    })
    public ResponseBean<OperatingItemEntity> detail(Long operItemId) {
        try {
            if (operItemId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            OperatingItemEntity entity = operatingItemService.getDetail(operItemId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("未查询到记录");
            }
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return ResponseBean.fail("获取详情异常");
        }
    }
}
